package com.smart.cardceureka;

import com.alibaba.fastjson.JSONObject;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.smart.common.utils.DateUtils;
import com.smart.common.utils.redis.RedisManager;
import com.smart.constants.EurekaServerStatus;
import com.smart.constants.RedisIndex;
import com.smart.enums.RedisKeyEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class EurekaStateChangeListener {

    @Autowired
    private RedisManager redisManager;
    
	//1服务断线/下线事件,服务被剔除
    @EventListener
    public void listen(EurekaInstanceCanceledEvent event) {
    	String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date());
        String appName = event.getAppName();
        String instanceId = event.getServerId();

        System.out.println("=============================");
        System.out.println(nowTime + "---服务下线："+instanceId+"----"+appName);

        JSONObject object = new JSONObject();
        object.put("instanceId",instanceId);
        object.put("appName",appName);
        object.put("refreshTime", DateUtils.getNowDateTimeStr());
        object.put("status", EurekaServerStatus.OFF_LINE);
        String regServerInstanceKey = RedisKeyEnum.EUREKA_SERVER_INSTANCE.getCode()+":"+appName;
        redisManager.setHashString(regServerInstanceKey,instanceId,object.toJSONString(), RedisIndex.INDEX_12);

        System.out.println("=============================");

    }
    //2服务注册
    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {

        InstanceInfo instanceInfo = event.getInstanceInfo();
    	System.out.println("=============================");
        System.out.println("服务注册:"+instanceInfo);

        String appName = instanceInfo.getAppName();
        String instanceId = instanceInfo.getInstanceId();
        JSONObject object = new JSONObject();
        object.put("instanceId",instanceId);
        object.put("appName",appName);
        object.put("refreshTime", DateUtils.getNowDateTimeStr());
        object.put("status", EurekaServerStatus.ON_LINE);

        String regServerSetKey = RedisKeyEnum.EUREKA_SERVER_SET.getCode();
        String regServerInstanceKey = RedisKeyEnum.EUREKA_SERVER_INSTANCE.getCode()+":"+appName;

        redisManager.sadd(regServerSetKey,appName,RedisIndex.INDEX_12);
        redisManager.setHashString(regServerInstanceKey,instanceId,object.toJSONString(), RedisIndex.INDEX_12);

    	System.out.println("=============================");

    }
    //3服务续约,这个默认是每1分钟检查一次
    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
    	String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date());

    	String appName = event.getAppName();
        String instanceId = event.getServerId();
        InstanceInfo instanceInfo = event.getInstanceInfo();
        System.out.println("=============================");
        JSONObject object = new JSONObject();
        object.put("instanceId",instanceId);
        object.put("appName",appName);
        object.put("refreshTime", DateUtils.getNowDateTimeStr());

        if(instanceInfo == null){
            //TODO 代表是新注册进来的服务?
            object.put("status", EurekaServerStatus.ON_LINE);
        }else{
            InstanceStatus status = instanceInfo.getStatus();
            if("UP".equals(status.name())){
                object.put("status", EurekaServerStatus.ON_LINE);
            }else{
                object.put("status", EurekaServerStatus.OFF_LINE);
            }
        }

        System.out.println(nowTime + "---服务续约："+instanceId+"----"+appName);


        String regServerSetKey = RedisKeyEnum.EUREKA_SERVER_SET.getCode();
        String regServerInstanceKey = RedisKeyEnum.EUREKA_SERVER_INSTANCE.getCode()+":"+appName;

        redisManager.sadd(regServerSetKey,appName,RedisIndex.INDEX_12);
        redisManager.setHashString(regServerInstanceKey,instanceId,object.toJSONString(), RedisIndex.INDEX_12);

        System.out.println("=============================");

    }
    //4注册中心启动事件
    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
    	System.out.println("=============================");
    	System.out.println("=========注册中心启动===============");
    	System.out.println("=============================");

    }
    //Server启动事件
    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        //Server启动
    	System.out.println("=============================");
    	System.out.println("=========Server启动===============");
    	System.out.println("=============================");
    	
    }
}
